package com.shawn.autoconfig.template;

import cn.hutool.core.collection.CollUtil;
import com.easemob.im.server.EMException;
import com.easemob.im.server.EMProperties;
import com.easemob.im.server.EMService;
import com.easemob.im.server.model.EMTextMessage;
import com.easemob.im.server.model.EMVoiceMessage;
import com.shawn.autoconfig.properties.CMProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;

import java.util.Set;

/**
 * 即时通讯服务
 *
 * @author lenovo
 * @date 2022/10/31
 */
@Slf4j
public class EMTemplate {

    private EMService emService;

    public EMTemplate(CMProperties cmProperties) {
        EMProperties emProperties = EMProperties.builder()
                .setAppkey(cmProperties.getAppKey())
                .setClientId(cmProperties.getClientId())
                .setClientSecret(cmProperties.getClientSecret())
                .build();
        this.emService = new EMService(emProperties);
    }
    //注册账号
    public boolean registeredMember(String username,String password){
        try {
            emService.user().create(username,password).block();
            return true;
        } catch (EMException e) {
            log.error("创建用户失败!==>{}",e.getMessage());
        }
        return false;
    }
    //添加好友
    public boolean addFriend(String username1,String username2){
        try {
            emService.contact().add(username1, username2).block();
            return true;
        } catch (EMException e) {
            log.error("添加联系人失败!==>{}",e.getMessage());
        }
        return false;
    }
    //删除好友
    public boolean removeFriend(String username1,String username2){
        try {
            emService.contact().remove(username1, username2).block();
            return true;
        } catch (EMException e) {
            log.error("删除联系人失败!==>{}",e.getMessage());
        }
        return false;
    }
    //发送消息
    public Boolean sendMessage(String username,String msgContent) {
        try {
            //接收人用户列表
            Set<String> set = CollUtil.newHashSet(username);
            //文本消息
            EMTextMessage message = new EMTextMessage().text(msgContent);
            //发送消息  from：admin是管理员发送
            emService.message().send("admin","users",
                    set,message,null).block();
            log.info("消息发送成功:{}",msgContent);
            return true;
        }catch (Exception e) {
            log.error("消息发送失败!==>{}",e.getMessage());
            return false;
        }
    }
    //发送语音
/*    public Boolean sendVoice(String username,String msgContent) {
        try {
            //接收人用户列表
            Set<String> set = CollUtil.newHashSet(username);
            //文本消息
            new EMVoiceMessage().bytes();
            //发送消息  from：admin是管理员发送
            emService.message().send("admin","users",
                    set,message,null).block();
            return true;
        }catch (EMException e) {
            log.error("消息发送失败!==>{}",e.getMessage());
        }
        return false;
    }*/
}
